package com.tjsinfo.tjsoa.fileDocument.bo;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.fileDocument.dao.IFileDocumentGroupDAO;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentFile;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentGroup;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentUser;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;

import common.Logger;

public class FileDocumentGroupBo implements IFileDocumentGroupBo {
	private IFileDocumentGroupDAO documentGroupDAO;
	private IQbcDao qbcDao;
	private IJDBCDao jdbcDao;
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	public static void setDeleteList(List deleteList) {
		FileDocumentGroupBo.deleteList = deleteList;
	}
	public void setDocumentGroupDAO(IFileDocumentGroupDAO documentGroupDAO) {
		this.documentGroupDAO = documentGroupDAO;
	}
	private static List	deleteList;
	public static List getDeleteList() {
		return deleteList;
	}
	
	public String[] getGroup(int user_id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentUser.class);
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		criteria1.add(Restrictions.eq("id", user_id));
		List<FileDocumentUser> list = qbcDao.getCriteria(criteria); 
		String[] strings = new String[list.size()*2];
		int i = 0;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileDocumentUser group = (FileDocumentUser) iterator.next();
			strings[i] = group.getFileDocumentGroup().getId().toString();
			i++;
			strings[i] = group.getFileDocumentGroup().getGroupName();
			i++;
		}
		return strings;
	}
	
	public String[] getAllGroup() {
		List<FileDocumentGroup> list = documentGroupDAO.findAll();
		
		String[] strings = new String[list.size()*2];
		int i = 0;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileDocumentGroup group = (FileDocumentGroup) iterator.next();
			strings[i] = group.getId().toString();
			i++;
			strings[i] = group.getGroupName();
			i++;
		}
		return strings;
	}
	
	public String insertGroup(String groupName,String downUserId) {
		FileDocumentGroup group = new FileDocumentGroup();
		String id = "";
		group.setGroupName(groupName);
		group.setCreateTime(new java.util.Date());
		String str = "error";
		try {
			id = documentGroupDAO.save(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (!"".equals(downUserId) && downUserId!=null) {
			try {   
			      String sql = "insert into dbo.t_fileDocument_user(id,group_id,user_id) VALUES(?,?,?)";   
			      PreparedStatement prest = jdbcDao.getConn().prepareStatement(sql);
			      String[] str1 = downUserId.split(",");
			      for (int i = 0; i < str1.length; i++) {
			    	  if (!"".equals(str1[i]) && str1[i]!=null){
			    		  prest.setString(1, UUID.randomUUID().toString().replace("-", ""));
			    		  prest.setString(2, id);
			    		  prest.setInt(3, Integer.valueOf(str1[i]));
			    		  prest.addBatch();
			    	  }
			      }
			      prest.executeBatch();
			      jdbcDao.closeConn();
			}catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return str;
	}
	
	public String updateGroup(String id,String groupName) {
		FileDocumentGroup group = new FileDocumentGroup();
		group.setId(id);
		group.setGroupName(groupName);
		String str = "error";
		try {
			documentGroupDAO.update(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	public String FirstDeleteGroup(String groupid,String dirName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentGroup.class);
		criteria.add(Restrictions.eq("id", groupid));
		
		ArrayList<FileDocumentGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		FileDocumentGroup group = list.iterator().next();
		DetachedCriteria criteria1 = DetachedCriteria.forClass(FileDocumentFile.class);
		criteria1.add(Restrictions.eq("fileDocumentGroup", group));
		ArrayList list1 = (ArrayList)qbcDao.getCriteria(criteria1);
		String str = "ok";
		if(list1.size() != 0){
			str = "haveUser";
		}else {
			this.deleteGroup(groupid);
		}
		return str;
	}

	public String deleteGroup(String id) {
		String sql = "delete from dbo.t_fileDocument_group where id='"+id+"'";
		jdbcDao.executeUpdate(sql);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
		String sql1 = "delete from dbo.t_fileDocument_user where group_id='"+id+"'";
		jdbcDao.executeUpdate(sql1);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
		return "ok";
	}
	
	public FileDocumentGroup findById(String id) {
		FileDocumentGroup group = documentGroupDAO.findById(id);
		return group;
	}
	
	public FileDocumentGroup findByMainGroup(int userid,String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentGroup.class);
		criteria.add(Restrictions.eq("groupName", name));
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		criteria1.add(Restrictions.eq("id", userid));
		ArrayList<FileDocumentGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		FileDocumentGroup group = list.iterator().next();
		return group;
	}
	
	public List<FileDocumentGroup> findByAll() {
		List<FileDocumentGroup> list = documentGroupDAO.findAll();
		return list;
	}
}
